<?php


namespace SidToken\Utils;


class TronUtil
{
    private $key = "d78027b3aa255032d29cc02dc85dd408";
    private $baseUrl = "http://92.204.132.11:33979";
    private $tag = "TAG";

    private function makeSign($params)
    {
        //签名步骤一：按字典序排序数组参数
        ksort($params);
        $string = $this->ToUrlParams($params);
        err_log("make sign:" . $string);
        return hash_hmac('md5', $string, $this->key);
    }

    public function ToUrlParams($params)
    {
        $string = '';
        if (!empty($params)) {
            $array = array();
            foreach ($params as $key => $value) {
                $array[] = $key . '=' . $value;
            }
            $string = implode("&", $array);
        }
        return $string;
    }

    public function getAddress($username)
    {
        $url = "/account/getaddress";
        $requestUrl = $this->baseUrl . $url;
        $params = [
            'tag' => $this->tag,
            'username' => $username,
        ];
        $sign = $this->makeSign($params);
        $params['sign'] = $sign;
        $result = curlPost($requestUrl, $params);
        tron_log("url:" . $requestUrl . ";params:" . json_encode($params) . ";result:" . $result);
        return $result;
    }

}